package leetcode.datastructure.queue;

import java.util.LinkedList;
import java.util.Queue;

public class Program225Queue2Stack {

    private int size;
    private Queue<Integer> oneQueue;
    private Queue<Integer> twoQueue;

    public Program225Queue2Stack() {
        size = 0;
        oneQueue = new LinkedList<>();
    }

    public void push(int x) {
        oneQueue.offer(x);
        size++;
    }

    public int pop() {
        if (size == 0) {
            return -1;
        }
        int i = 0;
        twoQueue = new LinkedList<>();
        while (i < size - 1) {
            twoQueue.offer(oneQueue.poll());
            i++;
        }
        int res = oneQueue.poll();
        oneQueue = twoQueue;
        size--;
        return res;
    }

    public int top() {
        if (size == 0) {
            return -1;
        }
        int i = 0;
        int res = 0;
        twoQueue = new LinkedList<>();
        while (i < size) {
            res = oneQueue.poll();
            twoQueue.offer(res);
            i++;
        }
        oneQueue = twoQueue;
        return res;
    }

    public boolean empty() {
        return size == 0;
    }

    public static void main(String[] args) {
        Program225Queue2Stack myStack = new Program225Queue2Stack();
        myStack.push(1);
        myStack.push(2);
        System.out.println(myStack.top()); // 返回 2
        System.out.println(myStack.pop()); // 返回 2
        System.out.println(myStack.empty()); // 返回 False
    }
}
